home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 11 object serialization / xmlserializationdemo / classes.vb < prev    next >
Text File  |  2002-03-16  |  4KB  |  159 lines

  1. Imports System.Xml.Serialization
  2.  
  3. ' a simple class for our serialization experiments
  4.  
  5. Public Class Customer
  6.     Public ID As Integer
  7.     Public Name As String
  8.     Public Address As String
  9.     Public City As String
  10.  
  11.     ' All XML-serializable classes must support a parameterless constructor.
  12.     Sub New()
  13.         ' nothing to do in this example.
  14.     End Sub
  15.  
  16.     Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String, ByVal city As String)
  17.         Me.ID = id
  18.         Me.Name = name
  19.         Me.Address = address
  20.         Me.City = city
  21.     End Sub
  22. End Class
  23.  
  24. ' a serializable class with attributes
  25.  
  26. <XmlRootAttribute("Customer", Namespace:="http://www.vb2themax.com", IsNullable:=False)> _
  27. Public Class Customer2
  28.  
  29.     <XmlAttributeAttribute("CustId")> Public ID As Integer
  30.  
  31.     <XmlElement("name")> Public Name As String
  32.  
  33.     <XmlIgnore()> Public Address As String
  34.  
  35.     <XmlElement("city", IsNullable:=False)> Public City As String
  36.  
  37.     <XmlText()> Public Notes As String
  38.  
  39.     ' All XML-serializable classes must support a parameterless constructor.
  40.     Sub New()
  41.         ' nothing to do in this example.
  42.     End Sub
  43.  
  44.     Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String, ByVal city As String)
  45.         Me.ID = id
  46.         Me.Name = name
  47.         Me.Address = address
  48.         Me.City = city
  49.     End Sub
  50. End Class
  51.  
  52. ' another serializable class with attributes
  53.  
  54. Public Class Customer3
  55.     <XmlAttributeAttribute("CustId", Namespace:="www.abc.com")> _
  56.     Public ID As Integer
  57.  
  58.     <XmlElement("name", Namespace:="www.abc.com")> _
  59.     Public Name As String
  60.  
  61.     <XmlIgnore()> _
  62.     Public Address As String
  63.  
  64.     <XmlElement("city", IsNullable:=False)> _
  65.     Public City As String
  66.  
  67.     <XmlText()> _
  68.     Public Notes As String
  69.  
  70.     ' All XML-serializable classes must support a parameterless constructor.
  71.     Sub New()
  72.         ' nothing to do in this example.
  73.     End Sub
  74.  
  75.     Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String, ByVal city As String)
  76.         Me.ID = id
  77.         Me.Name = name
  78.         Me.Address = address
  79.         Me.City = city
  80.     End Sub
  81. End Class
  82.  
  83. ' a Customer class that contains nested Order objects.
  84.  
  85. Public Class Customer4
  86.     <XmlArray("CustOrders"), XmlArrayItem("CustOrder", IsNullable:=True)> _
  87.     Public Orders(3) As Order
  88.  
  89.     <XmlAttributeAttribute("CustId", Namespace:="www.abc.com")> _
  90.     Public ID As Integer
  91.  
  92.     <XmlElement("name", Namespace:="www.abc.com")> _
  93.     Public Name As String
  94.  
  95.     <XmlIgnore()> _
  96.     Public Address As String
  97.  
  98.     <XmlElement("city", IsNullable:=False)> _
  99.     Public City As String
  100.  
  101.     <XmlText()> _
  102.     Public Notes As String
  103.  
  104.     ' All XML-serializable classes must support a parameterless constructor.
  105.     Sub New()
  106.         ' nothing to do in this example.
  107.     End Sub
  108.  
  109.     Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String, ByVal city As String)
  110.         Me.ID = id
  111.         Me.Name = name
  112.         Me.Address = address
  113.         Me.City = city
  114.     End Sub
  115. End Class
  116.  
  117. Public Class Order
  118.     <XmlAttributeAttribute("OrderId")> _
  119.     Public ID As Integer
  120.  
  121.     Public [Date] As Date               ' Note how we deal with a VB reserved word.
  122.     Public Total As Decimal
  123.  
  124.     ' All XML serializable classes must have a default constructor.
  125.     Sub New()
  126.         ' Nothing to do in this demo
  127.     End Sub
  128.  
  129.     Sub New(ByVal id As Integer, ByVal [date] As Date, ByVal total As Decimal)
  130.         Me.ID = id
  131.         Me.Date = [date]
  132.         Me.Total = total
  133.     End Sub
  134. End Class
  135.  
  136. ' these classes are used to explain XML serialization overriding
  137.  
  138. Public Class AddressBook
  139.     Public Name As String
  140.     Public Contacts() As Person
  141. End Class
  142.  
  143. Public Class Person
  144.     Public ID As Integer
  145.     Public Name As String
  146.     Public PhoneNumber As String
  147. End Class
  148.  
  149. Public Class Employee
  150.     Inherits Person
  151.     Public SSN As String
  152. End Class
  153.  
  154. Public Class CandidateEmployee
  155.     Inherits Person
  156.     Public InterviewDate As Date
  157. End Class
  158.  
  159.